package com.example.filedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {

    private static final String TAG ="tag";
    private TextView contextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intiView();
        intiSharedPreference();
        writeInnerFile();
        readInnerFile();


    }
    private void intiView(){
        Button button=findViewById(R.id.but_play_music);
        Button button1=findViewById(R.id.but_read_config);
        contextView=findViewById(R.id.text_config);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // InputStream is=getResources().openRawResource(R.raw.duang);
                MediaPlayer.create(MainActivity.this,R.raw.duang).start();
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // InputStream is= getResources().openRawResource( R.raw.config);
                try {
                    AssetManager am=getAssets();
                    InputStream ip=am.open("assetsConfug.txt");
                    BufferedReader reader=new BufferedReader(new InputStreamReader(ip));
                    StringBuffer sb=new StringBuffer();
                    String str=null;
                    while ((str=reader.readLine())!=null){
                        sb.append(str);
                    }
                    contextView.setText(sb.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


    }
    private String readFile(InputStream inputStream){

            byte[] bytes=new byte[1024];
            String str="";
            int a=0;
        try {
            while ((a=inputStream.read(bytes))!=-1){
                str=new String(bytes,0,a);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    private void intiSharedPreference(){
        //父类Context的方法
        //Fragment中需要通过requireActivity().getSharedPreference()调用
        //参数1 生成的xml文件名  参数2 访问权限
        //Context.MODE_PRIVATE 可读可写权限
        SharedPreferences sp=getSharedPreferences("test_share", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        //添加数据
        editor.putBoolean("test_boolean",true);
        editor.putFloat("test_float",3.14f);
        editor.putInt("test_int",123);
        editor.putLong("test_long",1380657880);
        editor.putString("test_string","test");
        //提交  commit为同步方法，数据量较大时，可能会卡顿主线程
        //apply为异步方法 通过后台线程提交 不影响主线程
        editor.apply();
        //获取数据
        SharedPreferences sp2=getSharedPreferences("test_share",Context.MODE_PRIVATE);
        String strData=sp2.getString("test_string","");
        boolean testBool=sp2.getBoolean("test_boolean",false);
        int testInt=sp2.getInt("test_int",0);
        float testFloat=sp2.getFloat("test_float",0.0f);
        long testLong=sp2.getLong("test_long",-1);

        Log.d(TAG,"SharedPreference test_string:"+strData);
        Log.d(TAG,"SharedPreference test_boolean:"+testBool);
        Log.d(TAG,"SharedPreference test_int:"+testInt);
        Log.d(TAG,"SharedPreference test_float:"+testFloat);
        Log.d(TAG,"SharedPreference test_long:"+testLong);
        

    }

   private void readInnerFile() {

       FileInputStream fs = null;
       try {
           fs = openFileInput("hello_internal_file.txt");
           BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fs));
           StringBuffer sb = new StringBuffer();
           String str = null;
           while ((str = bufferedReader.readLine()) != null) {
               sb.append(str);

           }
           contextView.setText(sb.toString());
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }


   }


    private void writeInnerFile() {
        //内部存储文件操作中
        //同一个文件使用Context.MODE.PRIVATE权限会覆盖原有内容
        //Context.MODE.APPEND追加内容
        FileOutputStream fs= null;
        try {
            fs = openFileOutput("hello_internal_file.txt", Context.MODE_PRIVATE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedWriter bf=new BufferedWriter(new OutputStreamWriter(fs));
        try {
            bf.write("hello world");
            bf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //方法2
       /* BufferedOutputStream bp=new BufferedOutputStream(fs);
        String text="hello world";
        try {
            bp.write(text.getBytes());
            bp.flush();
            bp.close();
        } catch (IOException e) {
            e.printStackTrace();
        }*/


    }

}